package com.zoro.jianyue.ui.fragment;

import android.graphics.Rect;
import android.provider.SyncStateContract;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zoro.jianyue.R;
import com.zoro.jianyue.bean.ImagesListEntity;
import com.zoro.jianyue.bean.ResponseImagesListEntity;
import com.zoro.jianyue.common.Constants;
import com.zoro.jianyue.common.OnCommonPageSelectedListener;
import com.zoro.jianyue.libs.adapter.ListViewDataAdapter;
import com.zoro.jianyue.libs.adapter.ViewHolderBase;
import com.zoro.jianyue.libs.adapter.ViewHolderCreator;
import com.zoro.jianyue.libs.pla.PLAAdapterView;
import com.zoro.jianyue.libs.pla.PLAImageView;
import com.zoro.jianyue.libs.pla.PLAMultiColumnListView;
import com.zoro.jianyue.presenter.ImagesListPresenter;
import com.zoro.jianyue.ui.base.BaseFragment;
import com.zoro.jianyue.utils.CommonUtils;
import com.zoro.jianyue.view.ImagesListView;
import com.zoro.jianyue.widgets.PLALoadMoreListView;
import com.zoro.jianyue.widgets.XSwipeRefreshLayout;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by CM-ZR on 2016/1/28.
 */
public class ImagesListFragment extends BaseFragment implements ImagesListView, OnCommonPageSelectedListener, PLAMultiColumnListView.OnLoadMoreListener, PLAAdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.fragment_images_list_swipe_layout)
    XSwipeRefreshLayout mXSwipeRefreshLayout;
    @Bind(R.id.fragment_images_list_list_view)
    PLALoadMoreListView mListView;
    /**
     * this variable must be initialized
     */
    private static String mCurrentImagesCategory = null;
    /**
     * the page number
     */
    private int mCurrentPage = 0;

    private ImagesListPresenter mImagesListPresenter = null;
    private ListViewDataAdapter<ImagesListEntity> mListViewAdapter = null;

    @Override
    protected int setContentViewLayoutId() {
        return R.layout.fragment_images_list;
    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {
        mCurrentImagesCategory = getResources().getStringArray(R.array.images_category_list_id)[0];

        mListViewAdapter = new ListViewDataAdapter<ImagesListEntity>(new ViewHolderCreator<ImagesListEntity>() {
            @Override
            public ViewHolderBase<ImagesListEntity> createViewHolder(int position) {
                return new ViewHolderBase<ImagesListEntity>() {

                    PLAImageView mItemImage;

                    @Override
                    public View createView(LayoutInflater layoutInflater) {
                        View convertView = layoutInflater.inflate(R.layout.list_item_images_list, null);
                        mItemImage = ButterKnife.findById(convertView, R.id.list_item_images_list_image);
                        return convertView;
                    }

                    @Override
                    public void showData(int position, ImagesListEntity itemData) {
                        int width = itemData.getThumbnailWidth();
                        int height = itemData.getThumbnailHeight();

                        String imageUrl = itemData.getThumbnailUrl();

                        if (!CommonUtils.isEmpty(imageUrl)) {
                            ImageLoader.getInstance().displayImage(imageUrl, mItemImage);
                        }

                        mItemImage.setImageWidth(width);
                        mItemImage.setImageHeight(height);
                    }
                };
            }
        });

        mListView.setOnItemClickListener(this);
        mListView.setOnLoadMoreListener(this);
        mListView.setAdapter(mListViewAdapter);

        mXSwipeRefreshLayout.setColorSchemeColors(R.color.gplus_color_1, R.color.gplus_color_2, R.color.gplus_color_3, R.color.gplus_color_4);
        mXSwipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }


    @Override
    public void onPageSelected(int position, String keywords) {
        mCurrentImagesCategory = keywords;
    }

    @Override
    public void onItemClick(PLAAdapterView<?> parent, View view, int position, long id) {
        Rect rect = new Rect();
        getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statubBarHeight = rect.top;

        int[] location = new int[2];
        view.getLocationOnScreen(location);
        location[1] += statubBarHeight;

        int width = view.getWidth();
        int height = view.getHeight();

        if (null != mListViewAdapter) {
            if (position >= 0 && position < mListViewAdapter.getDataList().size()) {
                mImagesListPresenter.onItemClickListener(position, mListViewAdapter.getDataList().get(position), location[0], location[1], width, height);
            }
        }
    }

    @Override
    public void onLoadMore() {
        mCurrentPage++;
        mImagesListPresenter.loadListData(TAG_LOG, Constants.EVENT_REFRESH_DATA, mCurrentImagesCategory, mCurrentPage, true);
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 0;
        mImagesListPresenter.loadListData(TAG_LOG, Constants.EVENT_REFRESH_DATA, mCurrentImagesCategory, mCurrentPage, true);
    }

    @Override
    public void showError(String msg) {
        if (null != mXSwipeRefreshLayout) {
            mXSwipeRefreshLayout.setRefreshing(false);
        }
        toggleShowError(true, msg, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImagesListPresenter.loadListData(TAG_LOG, Constants.EVENT_REFRESH_DATA, mCurrentImagesCategory, mCurrentPage, false);
            }
        });
    }


    @Override
    public void refreshListData(ResponseImagesListEntity responseImagesListEntity) {

    }

    @Override
    public void addMoreListData(ResponseImagesListEntity responseImagesListEntity) {

    }

    @Override
    public void navigateToImagesDetail(int position, ImagesListEntity entity, int x, int y, int width, int height) {

    }
}
